//
//  XGdemo.swift
//  TodayNews
//
//  Created by monkey on 2020/2/9.
//  Copyright © 2020 itcast. All rights reserved.
//

import Foundation

// MARK: - 网络相关

/// 服务器地址
let TodayNews_Base_URL: String = "https://is.snssdk.com/"
/// 设备id
let TodayNews_Device_Id: Int = 52103391187//6096495334
/// 未知
let TodayNews_iid: Int = 101841717944//5034850950

/// 我的页面设置列表接口
let TodayNews_Mine_Items_API: String = TodayNews_Base_URL + "user/tab/tabs/"
/// 我的关注接口
let TodayNews_Mine_Concern_API: String = TodayNews_Base_URL + "concern/v2/follow/my_follow/"
/// 用户详情接口
let TodayNews_User_Detail_API: String = TodayNews_Base_URL + "user/profile/homepage/v4/"
/// 用户推荐接口
let TodayNews_User_Recommend_API: String = TodayNews_Base_URL + "user/relation/user_recommend/v1/supplement_recommends/"
/// 关注用户接口
let TodayNews_Follow_Relation_API: String = TodayNews_Base_URL + "2/relation/follow/v2/"
/// 取消关注用户接口
let TodayNews_Unfollow_Relation_API: String = TodayNews_Base_URL + "2/relation/unfollow/"
/// 动态评论接口 动态cell如果是评论或引用则调用这个接口
let TodayNews_ReferenceStatus_Comment_API: String = TodayNews_Base_URL + "2/comment/v1/reply_list/"
/// 动态评论接口
let TodayNews_UserStatus_Comment_API: String = TodayNews_Base_URL + "article/v2/tab_comments/"

/// 首页分类标题接口
let TodayNews_News_Category_API: String = TodayNews_Base_URL + "article/category/get_subscribed/v1/"
/// 新闻列表接口
let TodayNews_News_List_API: String = TodayNews_Base_URL + "api/news/feed/v65/"

/// 西瓜视频分类标题接口
let TodayNews_Video_Category_API: String = TodayNews_Base_URL + "video_api/get_category/v1/"
/// 视频信息接口
let TodayNews_Video_Info_API: String = TodayNews_Base_URL + "video/urls/v/1/toutiao/mp4/"

// MARK: - Mine API

extension AlamofireTool
{
    /// 加载我的页面设置列表数据
    /// - Parameter completionHandle: 完成回调
    static func loadMineListData(completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        let parameters = requestParameters()
        AlamofireTool.request(method: .get, urlString: TodayNews_Mine_Items_API, parameters: parameters, completionHandle: completionHandle)
    }
    
    /// 加载我的关注数据
    /// - Parameter completionHandle: 完成回调
    static func loadMineConcernData(completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        let parameters = requestParameters()
        AlamofireTool.request(method: .get, urlString: TodayNews_Mine_Concern_API, parameters: parameters, completionHandle: completionHandle)
    }
    
    /// 加载用户详情数据
    /// - Parameters:
    ///   - userId: 用户ID
    ///   - completionHandle: 完成回调
    static func loadUserDetailData(userId: UInt64,completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        let parameters = requestParameters(["user_id": userId])
        AlamofireTool.request(method: .get, urlString: TodayNews_User_Detail_API, parameters: parameters, completionHandle: completionHandle)
    }
    
    /// 加载推荐用户数据
    /// - Parameters:
    ///   - userId: 用户ID
    ///   - completionHandle: 完成回调
    static func loadUserRecommendData(userId: UInt64,completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        let parameters = requestParameters(["follow_user_id": userId])
        // 必须指定请求头
        let headers = ["user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"]
        AlamofireTool.request(method: .get, urlString: TodayNews_User_Recommend_API, parameters: parameters, headers: headers, completionHandle: completionHandle)
    }
    
    /// 关注用户
    /// - Parameters:
    ///   - userId: 用户ID
    ///   - completionHandle: 完成回调
    static func loadFollowRelationData(userId: UInt64,completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        let parameters = requestParameters(["user_id": userId])
        AlamofireTool.request(method: .get, urlString: TodayNews_Follow_Relation_API, parameters: parameters, completionHandle: completionHandle)
    }
    
    /// 取消关注用户
    /// - Parameters:
    ///   - userId: 用户ID
    ///   - completionHandle: 完成回调
    static func loadUnfollowRelationData(userId: UInt64,completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        let parameters = requestParameters(["user_id": userId])
        AlamofireTool.request(method: .get, urlString: TodayNews_Unfollow_Relation_API, parameters: parameters, completionHandle: completionHandle)
    }
    
    /// 加载引用动态评论数据
    /// - Parameters:
    ///   - statusId: 动态id
    ///   - offset: 偏移量
    ///   - completionHandle: 完成回调
    static func loadReferenceStatusCommentData(statusId: UInt64,offset: Int, completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        let parameters = requestParameters(["id": statusId, "offset": offset])
        AlamofireTool.request(method: .get, urlString: TodayNews_ReferenceStatus_Comment_API, parameters: parameters, completionHandle: completionHandle)
    }
    /// 加载用户动态评论数据
    /// - Parameters:
    ///   - statusId: 动态id
    ///   - offset: 偏移量
    ///   - completionHandle: 完成回调
    static func loadUserStatusCommentData(statusId: UInt64,offset: Int, completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        
        let parameters = requestParameters(["group_id": statusId,
                                            "offset": offset,
                                            "version_code": "7.6.1",
                                            "channel": "App Store",
                                            "device_platform": "iphone",
                                            "device_type": "iPhone 6S"])
        AlamofireTool.request(method: .get, urlString: TodayNews_UserStatus_Comment_API, parameters: parameters, completionHandle: completionHandle)
    }
    
    /// 拼接请求参数
    /// - Parameter parameters: 请求参数
    private static func requestParameters(_ parameters: [String: Any]? = nil) -> [String: Any]
    {
        var dict: [String: Any] = ["device_id": TodayNews_Device_Id,
                                   "iid": TodayNews_iid]
        if let parameters = parameters {
            dict.merge(parameters) { (current, new) -> Any in
                return current
            }
        }
        
        return dict
    }
}

// MARK: - Home API

extension AlamofireTool
{
    /// 加载首页新闻分类标题数据
    /// - Parameter completionHandle: 完成回调
    static func loadNewsCategoriesData(completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        let parameters = requestParameters(["aid": 10])
        AlamofireTool.request(method: .get, urlString: TodayNews_News_Category_API, parameters: parameters, completionHandle: completionHandle)
    }
    
    static func loadNewsListData(category: String,min_behot_time: TimeInterval = 0,max_behot_time:TimeInterval = 0, completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        var parameters:[String: Any] = ["category": category]
        if min_behot_time > 0 {
            parameters["min_behot_time"] = min_behot_time // 下拉刷新参数
        } else if max_behot_time > 0 {
            parameters["max_behot_time"] = max_behot_time
        }
        
        AlamofireTool.request(method: .get, urlString: TodayNews_News_List_API, parameters: parameters, completionHandle: completionHandle)
    }
}

// MARK: - Video API

extension AlamofireTool
{
    /// 加载西瓜视频分类标题数据
    /// - Parameter completionHandle: 完成回调
    static func loadVideoCategoriesData(completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {
        AlamofireTool.request(method: .get, urlString: TodayNews_Video_Category_API, completionHandle: completionHandle)
    }
    
    static func loadVideoInfoData(videoId: String,completionHandle: @escaping(Any?,Error?) -> Void) -> Void
    {        
        let rcode = 167311424
        let videoInfo = "video/urls/v/1/toutiao/mp4/"
        let url = String(format: "/%@%@?r=%d", videoInfo,videoId,rcode)
        var crc32 = Int(AlamofireTool.crc32(url))
        if crc32 < 0 {
            crc32 += 0x100000000
        }

        AlamofireTool.request(method: .get, urlString: TodayNews_Video_Info_API + videoId, parameters: ["r": rcode, "s": crc32], completionHandle: completionHandle);
    }
}
